/*
 * Created on 2005-3-12
 *
 */
package bma.common.langutil.bean.impl;

import java.util.Map;

import bma.common.langutil.bean.AccessType;
import bma.common.langutil.bean.BeanMap;

/**
 * 
 * @author guanzhong
 * @since 1.0 (imported from BMA.ESP Project)
 *
 */
public class BeanMapNative extends BeanMapAbstract implements BeanMap {

	public BeanMapNative(Class prototype) {
		super(prototype);
	}

	@Override
	public boolean accessible(AccessType type) {

		switch (type) {
		case MAP_GET:
		case SIZE:
		case MAP_REMOVE:
		case MAP_SET:
		case MAP_KEYS:
			return true;
		default:
			return false;
		}
	}

	@Override
	public Object get(Object thisObject, String key) {
		if (thisObject == null) {
			throw new NullPointerException("this object");
		}
		if (!(thisObject instanceof Map)) {
			throw new IllegalArgumentException("Map.class");
		}
		Map mo = (Map) thisObject;
		return mo.get(key);
	}

	@Override
	public int size(Object thisObject) {
		if (thisObject == null) {
			throw new NullPointerException("this object");
		}
		if (!(thisObject instanceof Map)) {
			throw new IllegalArgumentException("Map.class");
		}
		Map mo = (Map) thisObject;
		return mo.size();
	}

	@Override
	public void remove(Object thisObject, String key) {
		if (thisObject == null) {
			throw new NullPointerException("this object");
		}
		if (!(thisObject instanceof Map)) {
			throw new IllegalArgumentException("Map.class");
		}
		Map mo = (Map) thisObject;
		mo.remove(key);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void set(Object thisObject, String key, Object value) {
		if (thisObject == null) {
			throw new NullPointerException("this object");
		}
		if (!(thisObject instanceof Map)) {
			throw new IllegalArgumentException("Map.class");
		}
		Map mo = (Map) thisObject;
		mo.put(key, value);
	}

	@Override
	public String[] keys(Object thisObject) {
		if (thisObject == null) {
			throw new NullPointerException("this object");
		}
		if (!(thisObject instanceof Map)) {
			throw new IllegalArgumentException("Map.class");
		}
		Map mo = (Map) thisObject;
		String[] r = new String[mo.size()];
		int idx = 0;
		for (Object key : mo.keySet()) {
			r[idx++] = key.toString();
		}
		return r;
	}

}
